package com.goldman.oss.common.configuration;

import com.amazonaws.services.s3.AmazonS3;
import com.goldman.oss.common.internal.processor.context.OssCommonApplicationContextHolder;
import com.goldman.oss.common.internal.processor.context.ProcessorContextInitializer;
import com.goldman.oss.common.platform.FileStorage;
import com.goldman.oss.common.platform.impl.AwsS3FileStorage;
import com.goldman.oss.common.platform.impl.HuaweiObsFileStorage;
import com.obs.services.ObsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Configuration
@EnableConfigurationProperties(OssCommonProperties.class)
public class OssCommonAutoConfiguration {

    /**
     * obs
     */
    @Bean(name = "huaweiObsFileStorages")
    @ConditionalOnClass(ObsClient.class)
    public List<FileStorage> huaweiObsFileStorages(OssCommonProperties ossCommonProperties) {
        log.info("对象存储平台配置：huaweiObsFileStorage:{}", ossCommonProperties.getHuaweiObs());
        return ossCommonProperties.getHuaweiObs().stream()
                .filter(OssCommonProperties.HuaweiObs::getEnable).map(huaweiObs -> {
                    HuaweiObsFileStorage storage = new HuaweiObsFileStorage();
                    storage.setBusinessCode(huaweiObs.getBusinessCode());
                    storage.setAccessKey(huaweiObs.getAccessKey());
                    storage.setSecretKey(huaweiObs.getSecretKey());
                    storage.setEndPoint(huaweiObs.getEndPoint());
                    storage.setBucketName(huaweiObs.getBucketName());
                    storage.setDomain(huaweiObs.getDomain());
                    storage.setBasePath(huaweiObs.getBasePath());
                    storage.setDefaultExpirationInSeconds(huaweiObs.getDefaultExpirationInSeconds());
                    return storage;
                }).collect(Collectors.toList());
    }


    /**
     * s3
     */
    @Bean(name = "awsS3FileStorages")
    @ConditionalOnClass(AmazonS3.class)
    public List<FileStorage> awsS3FileStorages(OssCommonProperties ossCommonProperties) {
        log.info("对象存储平台配置：awsS3FileStorage:{}", ossCommonProperties.getAwsS3());
        return ossCommonProperties.getAwsS3().stream()
                .filter(OssCommonProperties.AwsS3::getEnable).map(awsS3 -> {
                    AwsS3FileStorage storage = new AwsS3FileStorage();
                    storage.setBusinessCode(awsS3.getBusinessCode());
                    storage.setAccessKey(awsS3.getAccessKey());
                    storage.setSecretKey(awsS3.getSecretKey());
                    storage.setRegion(awsS3.getRegion());
                    storage.setEndPoint(awsS3.getEndPoint());
                    storage.setBucketName(awsS3.getBucketName());
                    storage.setDomain(awsS3.getDomain());
                    storage.setBasePath(awsS3.getBasePath());
                    storage.setDefaultExpirationInSeconds(awsS3.getDefaultExpirationInSeconds());
                    return storage;
                }).collect(Collectors.toList());

    }

    @Bean
    public OssCommonApplicationContextHolder ossCommonApplicationContextHolder() {
        return new OssCommonApplicationContextHolder();
    }

    @Bean(destroyMethod = "destroy")
    public ProcessorContextInitializer processorContextInitializer() {
        return new ProcessorContextInitializer();
    }
}
